package com.rykj.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.lcw.library.imagepicker.utils.Util;
import com.rykj.R;
import com.rykj.widget.bottomnavigation.BottomLayoutTextView;
import com.rykj.widget.bottomnavigation.BottomObservable;
import com.rykj.widget.bottomnavigation.BottomObserver;

import java.util.Arrays;
import java.util.List;

import androidx.core.content.ContextCompat;

/**
 * Description ：
 * <p/>
 * Created by ycw on 2017/5/16.
 */

public class TabSwitchButton extends LinearLayout {

    /**
     * 圆角半径内
     */
    private float radius;

    /**
     * 按钮的文字颜色
     */
    private ColorStateList btnTextColor;
    /**
     *
     */
    private int color;

    private BottomObservable mObservable = new BottomObservable();

    private List<CharSequence> btnTextList;

    private CharSequence[] btnTexts;

    private ItemClick mItemClick;

    private OnClickListener mItemTabButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = (int) v.getTag();
            if (mItemClick != null) {
                mItemClick.clickButton(v, i);
            }
            mObservable.notifyObservers((BottomObserver) v);
        }
    };
    private LayoutParams mItemParams;

    public TabSwitchButton(Context context) {
        this(context, null);
    }

    public TabSwitchButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabSwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs, defStyleAttr);
    }

    private void initView(AttributeSet attrs, int defStyleAttr) {
        mItemParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);

        TypedArray a = getResources().obtainAttributes(attrs, R.styleable.TabSwitchButton);
        if (a != null) {
            btnTextColor = a.getColorStateList(R.styleable.TabSwitchButton_tab_switch_text_color);
            btnTexts = a.getTextArray(R.styleable.TabSwitchButton_tab_switch_text_array);
            radius = a.getDimensionPixelSize(R.styleable.TabSwitchButton_tab_switch_radius, 0);
            color = a.getColor(R.styleable.TabSwitchButton_tab_switch_color,
                    ContextCompat.getColor(getContext(), R.color.default_color));
            a.recycle();
        }

        setBtnTexts(btnTexts);
        // setBackgroundDrawable(ContextCompat.getDrawable(getContext(),R.drawable.corner_rectangle_shape));
    }

    /**
     *
     */
    public void setBtnTexts(CharSequence[] btnTexts) {
        if (btnTexts != null) {
            setBtnTexts(Arrays.asList(btnTexts));
        }
    }

    private Drawable getItemBackground(int position, int size) {
        int lastPosition = size - 1;
        Drawable drawable;
        if (position == 0) {
            drawable = getLeftDrawable();
        } else if (position == lastPosition) {
            drawable = getRightDrawable();
        } else {
            drawable = getCentreDrawable();
        }
        return drawable;
    }

    private Drawable getLeftDrawable() {
        StateListDrawable leftStateListDrawable = new StateListDrawable();
        GradientDrawable leftNormalDrawable = new GradientDrawable();
        GradientDrawable leftSelectDrawable = new GradientDrawable();

        leftNormalDrawable.setCornerRadii(new float[]{radius, radius, 0, 0, 0, 0, radius, radius});
        leftNormalDrawable.setStroke(Util.dp2px(getContext(), 1), color);
        leftSelectDrawable.setCornerRadii(new float[]{radius, radius, 0, 0, 0, 0, radius, radius});
        leftSelectDrawable.setColor(color);

        leftStateListDrawable
                .addState(new int[]{android.R.attr.state_selected}, leftSelectDrawable);
        leftStateListDrawable.addState(new int[]{}, leftNormalDrawable);
        return leftStateListDrawable;
    }

    private Drawable getRightDrawable() {
        StateListDrawable rightStateListDrawable = new StateListDrawable();
        GradientDrawable rightNormalDrawable = new GradientDrawable();
        GradientDrawable rightSelectDrawable = new GradientDrawable();

        rightNormalDrawable.setCornerRadii(new float[]{0, 0, radius, radius, radius, radius, 0, 0});
        rightNormalDrawable.setStroke(Util.dp2px(getContext(), 1), color);
        rightSelectDrawable.setCornerRadii(new float[]{0, 0, radius, radius, radius, radius, 0, 0});
        rightSelectDrawable.setColor(color);

        rightStateListDrawable
                .addState(new int[]{android.R.attr.state_selected}, rightSelectDrawable);
        rightStateListDrawable.addState(new int[]{}, rightNormalDrawable);
        return rightStateListDrawable;
    }

    private Drawable getCentreDrawable() {
        StateListDrawable centreStateListDrawable = new StateListDrawable();
        GradientDrawable centreNormalDrawable = new GradientDrawable();
        GradientDrawable centreSelectDrawable = new GradientDrawable();

        centreNormalDrawable.setCornerRadius(0);
        centreNormalDrawable.setStroke(Util.dp2px(getContext(), 1), color);
        centreSelectDrawable.setCornerRadius(0);
        centreSelectDrawable.setColor(color);

        centreStateListDrawable
                .addState(new int[]{android.R.attr.state_selected}, centreSelectDrawable);
        centreStateListDrawable.addState(new int[]{}, centreNormalDrawable);
        return centreStateListDrawable;
    }

    public void setItemClick(ItemClick itemClick) {
        mItemClick = itemClick;
    }

    public void setClickItem(int index) {
        if (mItemClick != null) {
            mObservable.notifyObservers((BottomObserver) getChildAt(index));
            mItemClick.clickButton(getChildAt(index), index);
        }
    }

    public List<CharSequence> getBtnTexts() {
        return btnTextList;
    }

    /**
     *
     */
    public void setBtnTexts(List<CharSequence> btnTexts) {
        this.btnTextList = btnTexts;
        removeAllViews();
        for (int i = 0; i < btnTexts.size(); i++) {
            BottomLayoutTextView view = new BottomLayoutTextView(getContext());
            view.setText(btnTexts.get(i));
            view.setTag(i);
            view.setPadding((int) radius * 2, (int) radius, (int) radius * 2, (int) radius);
            view.setGravity(Gravity.CENTER);
            view.setOnClickListener(mItemTabButtonListener);
            view.setBackgroundDrawable(getItemBackground(i, btnTexts.size()));
            view.setTextColor(btnTextColor);
            mObservable.addObserver(view);
            addView(view, mItemParams);
        }
        mObservable.notifyObservers((BottomObserver) getChildAt(0));
        invalidate();
    }

    public interface ItemClick {
        void clickButton(View v, int position);
    }


}
